//https://leetcode.cn/problems/longest-common-prefix/

//解法1
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        string ret = strs[0];
        for (int i = 1; i < strs.size(); i++)
        {
            ret = findcommon(ret, strs[i]);
        }

        return ret;

    }

    string findcommon(string& s1, string& s2)
    {
        int i = 0;
        while (i < min(s1.size(), s2.size()) && s1[i] == s2[i])
        {
            i++;
        }

        return s1.substr(0, i);
    }
};

//解法2
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        for (int i = 0; i < strs[0].size(); i++)
        {
            for (int j = 1; j < strs.size(); j++)
            {
                if (i >= strs[j].size() || strs[0][i] != strs[j][i])
                    return strs[0].substr(0, i);
            }
        }

        return strs[0];

    }
};